package com.openxt.nbzj.remoting;

import android.content.Context;
import android.widget.Toast;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import qs.openxt.libs.progress.Wait;
import qs.openxt.libs.remoting.Client;
import qs.openxt.libs.util.ActivityUtil;

/**
 * Created by 颖杰_2 on 2016/7/7.
 */
public class RemotingEx {
    private Context context;
    protected String serverAddress = "";
    private String actionName;
    private boolean isShowDialogProgress = true;

    private OnActionListener listener = null;

    public RemotingEx(Context context, OnActionListener l, String actionName) {
        this.context = context;
        this.listener = l;
        this.actionName = actionName;
    }

    public void setServerAddress(String serverAddress) {
        this.serverAddress = serverAddress;
    }

    public void setShowDialogProgress(boolean isShowDialogProgress) {
        this.isShowDialogProgress = isShowDialogProgress;
    }

    public void doRequest(JSONObject params) {
        if (context != null && !ActivityUtil.isNetworkAvailable(context)) {
            Toast.makeText(context, context.getString(qs.openxt.libs.R.string.isNetworkAvailable), Toast.LENGTH_SHORT).show();
            return;
        }

        if (context != null && isShowDialogProgress)
            Wait.sharedInstance().start(context, "正在通讯...，请稍候");

        try {
            ClientEx client = new ClientEx(this);
            client.setServerAddress(serverAddress);
            doClient(client, params);
        } catch (Exception e) {
            Wait.sharedInstance().stop();
        }
    }

    private void doClient(ClientEx client, JSONObject args) throws Exception {
        try {
            /*
            if (args != null) {
                Object data = args.opt("data");
                if (data instanceof JSONObject) {
                    Map<String, String> param = new HashMap<String, String>();
                    JSONObject $data = (JSONObject) data;
                    Iterator it = $data.keys();
                    while (it.hasNext()) {
                        String key = (String) it.next();
                        param.put(key, $data.optString(key));
                    }
                    client.request(param);
                } else
                    client.request(data);
            } else
                client.request(null);*/
            Object config = null;
            if (args != null) {
                Object data = args.get("data");
                if (data instanceof JSONObject) {
                    Map<String, String> param = new HashMap<>();
                    JSONObject $data = (JSONObject) data;
                    Iterator it = $data.keySet().iterator();
                    while (it.hasNext()) {
                        String key = (String) it.next();
                        param.put(key, $data.getString(key));
                    }
                    config = param;
                } else
                    config = data;
            }
            JSONObject json = new JSONObject();
            json.put("client", client);
            json.put("args", config);
            BusinessExTask task = new BusinessExTask(this);
            task.execute(json);
        } catch (Exception e) {
            try {
                JSONObject json = new JSONObject();
                json.put("status", Client.STATUS_ERR);
                json.put("Message", e.getMessage());
            } catch (Exception e0) {
            }
        }
    }

    public void doRequest(JSONObject params, String method) {
        if (context != null && !ActivityUtil.isNetworkAvailable(context)) {
            Toast.makeText(context, context.getString(qs.openxt.libs.R.string.isNetworkAvailable), Toast.LENGTH_SHORT).show();
            onError(null);
            return;
        }

        if (context != null && isShowDialogProgress)
            Wait.sharedInstance().start(context, "正在通讯...，请稍候");

        try {
            ClientEx client = new ClientEx(this);
            client.setMethod(method);
            client.setServerAddress(serverAddress);
            doClient(client, params);
        } catch (Exception e) {
            Wait.sharedInstance().stop();
        }
    }

    public void onSuccess(String reVal) {
        if(isShowDialogProgress)
            Wait.sharedInstance().stop();

        if (listener != null) {
            listener.onActionCompleted(reVal, actionName);
        }
    }

    public void onError(String errorVal) {
        if(isShowDialogProgress)
            Wait.sharedInstance().stop();

        if (listener != null) {
            listener.onActionFailed(errorVal, actionName);
        }
    }

    public interface OnActionListener {
        public void onActionCompleted(String reVal, String actionName);

        public void onActionFailed(String errorVal, String actionName);
    }
}
